AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "common/warning.wav" )
util.PrecacheSound( "Airboat_engine_idle" )
util.PrecacheSound( "Airboat_engine_stop" )
util.PrecacheSound( "apc_engine_start" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:SetModel( "models/props_c17/furnitureboiler001a.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetMaterial("models/props_combine/combinethumper002");
	local phys = self.Entity:GetPhysicsObject()
	self.NextThink = CurTime() + 3
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(1500)
	end
	self.Active = true
	self.val1 = 0
	self.val2 = 0
	LS_RegisterEnt(self.Entity)
	RD_AddResource(self.Entity, "energy", 0)
	RD_AddResource(self.Entity, "rawwater", 10)
	RD_AddResource(self.Entity, "water", 0)
	RD_AddResource(self.Entity, "suction", 0)

	self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" })	
	self.Inputs = Wire_CreateInputs(self.Entity, { "On" })		
end

function Startsound()

end

function Stopsound()

end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 75

	local ent = ents.Create( "pump_small" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Use()
	if (CurTime() >= self.NTog) then
		if (self.Active == true) then
			self.Active = false
			Stopsound()
		else
			self.Active = true
			Startsound()
		end
		self.NTog = CurTime() + 1
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value > 0) then
			if ( self.Active == false ) then
				self.Active = true
				Startsound()
			end
		else
			if ( self.Active == true ) then
				self.Active = false
				Stopsound()
			end
		end
	end

end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end

function ENT:Think()
	self.val1 = RD_GetResourceAmount(self.Entity, "energy")
	self.val2 = RD_GetResourceAmount(self.Entity, "rawwater")

	Wire_TriggerOutput(self.Entity, "Out", self.val2)

	if ( self.Active == true ) then
		self:SetOverlayText( "Small Water Pump\n(ON)\nEnergy: " .. self.val1)
	else
		self:SetOverlayText( "Small Water Pump\n(OFF)\nEnergy: " .. self.val1)
	end

	if (self.Active == true) then
		if (self.val1 > 15) then
			RD_ConsumeResource(self, "energy", 5)
			RD_ConsumeResource(self, "rawwater", self.val2)
			RD_SupplyResource(self.Entity, "water", self.val2)
			RD_SupplyResource(self.Entity, "suction", 10)
		else
			self.Entity:EmitSound( "common/warning.wav" )
			self.Active = false
			Stopsound()
		end
	end
	
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
end
